package com.xiaohudie.SGGJUC;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Author xiao hu die
 * @Date 2022/3/12 11:16
 * @Version 1.0
 */
public class TestFuturn {
    public static void main(String[] args) {
        CompletableFuture<Void> completableFuture =CompletableFuture.runAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        });
        CompletableFuture<String> completableFuture1 =CompletableFuture.supplyAsync(()->{
            String name = Thread.currentThread().getName();
            int  i =10/0;
            System.out.println(name);
            return name;
        });
        try {
            System.out.println(completableFuture1.whenComplete((x, y) -> {
                System.out.println(x + "-->" + x);
                System.out.println(y + "-->" + y);
            }).exceptionally((e) -> {
                System.out.println(e.getMessage());
                return "出错了";
            }).get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName());
        try {
             completableFuture.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
